package com.cloud.commana.ser.impl;

import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.cloud.commana.dto.ComConstant;
import com.cloud.commana.ser.IUploadService;
import com.cloud.util.base.RetType;
import com.cloud.util.date.DateUtil;
import com.cloud.util.other.StringUtil;
import com.cloud.util.other.Upload;

/**
 * 文件上传
 * @author gaof
 * @date 2017年2月6日
 */
@Service
public class UploadService implements IUploadService {

	/**
	 * 文件上传
	 * @param file
	 * @param request
	 * @return
	 */
	@Override
	public RetType uplaodFile(MultipartFile[] file, HttpServletRequest request) {
		
		RetType retType = new RetType();
		
		//文件校验
		if(file == null || file.length == 0){
			retType.setErrCode(RetType.RET_FAILE);
			retType.setErrMsg("上传文件为空，上传失败");
			
			return retType;
		}
		for (MultipartFile myfile : file) {
			if(myfile == null || myfile.isEmpty()){
				retType.setErrCode(RetType.RET_FAILE);
				retType.setErrMsg("上传文件为空，上传失败");
				
				return retType;
			}
		}
		
		String retUploadUrl = "";
		String nowDateYMD = DateUtil.getDateByYMD();//当前时间年月日
		// 生成随机数[1,999]的随机数
		int max = 999;
		int min = 1;
		Random random = new Random();
		int randomNum = random.nextInt(max) % (max - min + 1) + min;
		String nowDateYMDHMS = DateUtil.formatDateTimeTo14b(DateUtil
						.getNowStr()) + randomNum;//当前时间年与日加三位随机数
		//真实路径
//		String realPath = request.getSession().getServletContext()
//				.getRealPath("")
//				+ "/"
//				+ ComConstant.FILE_UPLOAD
//				+ "/"
//				+ nowDateYMD + "/";
		String realPath="D:/lgga_imges";
		//获取保存表的保存的文件名称
		String savetablefilename = request.getParameter("savetablefilename");
		realPath +="/"+savetablefilename;
		//存放路径
		String saveUrl = request.getContextPath()
				+ "/" + ComConstant.FILE_UPLOAD + "/"
				+ nowDateYMD + "/";
		
		//遍历文件储存
		for(int i = 0; i < file.length; i ++){
			MultipartFile myfile = file[i];
			retUploadUrl = uploadForImage(myfile, retUploadUrl, realPath, saveUrl, nowDateYMDHMS, i);
		}
		
		// 截取最后一个逗号
		if (!StringUtil.isEmpty(retUploadUrl)) {
			retUploadUrl = retUploadUrl.substring(0, retUploadUrl.length() - 1);
		}
		
		retType.setData(savetablefilename+"/"+retUploadUrl);
		retType.setErrCode(RetType.RET_SUCCESS);
		
		return retType;
	}

	/**
	 * 保存文件
	 * @param i 
	 * @param nowDateYMDHMS 
	 * @param saveUrl 
	 * @param realPath
	 * @param retUploadUrl 
	 * @param myfile 
	 * @return
	 */
	private String uploadForImage(MultipartFile myfile,
			String retUploadUrl, String realPath, String saveUrl,
			String nowDateYMDHMS, int i) {
		
		// 获取文件后缀名
		String fileType = "png";//Upload.getFileType(myfile.getOriginalFilename());
		fileType = fileType.toLowerCase();
		
		File files = new File(realPath);
		if (!files.exists()) {
			files.mkdirs();
		}
		
		if(!StringUtil.isEmpty(fileType)){
			fileType = "." + fileType;
		}

		// 这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉，我是看它的源码才知道的
		try {
			FileUtils.copyInputStreamToFile(
					myfile.getInputStream(), new File(realPath,
							nowDateYMDHMS + i + fileType));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//retUploadUrl += saveUrl + nowDateYMDHMS + i + fileType + ",";
		retUploadUrl += nowDateYMDHMS + i + fileType + ",";
		//retUploadUrl += nowDateYMDHMS + i + fileType;
		return retUploadUrl;
	}
	
}
